package com.joonggo.gip.controls.group;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.joonggo.gip.controls.ControllerTemplate;
import com.joonggo.gip.controls.ExecuteDetailFuncDetailStatement;
import com.joonggo.gip.dao.GroupDao;
import com.joonggo.gip.service.GroupService;
import com.joonggo.gip.vo.Group;
import com.joonggo.gip.vo.User;

@Controller
@RequestMapping("/group")
public class GroupController {
	@Autowired	GroupService groupService;
	@Autowired	GroupDao groupDao;
	@Autowired ControllerTemplate mainFunc;

	@RequestMapping(value="/getGroup",method=RequestMethod.POST)
	@ResponseBody
	public HashMap<String,Object> getGroup(final String group_Name) throws Exception {
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				Group group = groupDao.getGroup(group_Name);
				responseResult.put("group", group);
			}
		});
	}
	

	@RequestMapping(value="/searchGroup",method=RequestMethod.POST)
	@ResponseBody
	public HashMap<String,Object>searchGroup(final String group_Name) throws Exception {
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			
			
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				
				System.out.println("\n\n\n일단 controller에 들어오는지 봅시다>!");
				System.out.println(group_Name);
				System.out.println(groupDao.getsearchGroup(group_Name));
				
				responseResult.put("groupSearchList",groupDao.getsearchGroup(group_Name));

			}
		});
	}
	@RequestMapping(value="/getGroupByNum",method=RequestMethod.POST)
	@ResponseBody
	public HashMap<String,Object> getGroupByNum(final int group_No, final ServletRequest request) throws Exception {

		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				Group group = groupDao.getGroupByNum(group_No);
				System.out.println("groupAdmin : " + group.getGroup_Admin());
				HttpServletRequest req = (HttpServletRequest)request;
				HttpSession session= req.getSession();
				session.setAttribute("curGroup", group);
				
				//System.out.println("Session 객체에 들어간 curGroup : " + group);
				responseResult.put("group", group);
			}
		});
	}

	@RequestMapping(value="/addGroup",method=RequestMethod.POST)
	@ResponseBody
	public HashMap<String,Object> add(final Group group, BindingResult result,final ServletRequest request) throws Exception {
	//	System.out.println("groupInfo : " + group.getGroup_Inf());
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				HttpServletRequest req = (HttpServletRequest)request;
				HttpSession session= req.getSession();
				User loginedUser = (User)session.getAttribute("loginedUser");
				group.setGroup_Admin(loginedUser.getUser_Id());
				groupService.addGroup(group);
			}
		});
	}
	
	@RequestMapping(value="/addGroupWaiting",method=RequestMethod.POST)
	@ResponseBody
	public HashMap<String,Object> addGroupWaiting(final String group_Answer, final ServletRequest request) throws Exception {
		System.out.println("group_Answer : " + group_Answer);
		return mainFunc.getDataTemplate(new ExecuteDetailFuncDetailStatement() {
			@Override
			public void executeFunc(HashMap<String, Object> responseResult) throws Exception {
				HttpServletRequest req = (HttpServletRequest)request;
				HttpSession session= req.getSession();
				HashMap<String, Object> parameterValue = new HashMap<String, Object>();
				
				User loginedUser = (User)session.getAttribute("loginedUser");
				Group curGroup = (Group)session.getAttribute("curGroup");
				
				System.out.println("Session 객체에 들어간 curGroup : " + curGroup);
				System.out.println("loginedUser : " + loginedUser.getUser_Id());
				System.out.println("curGroup : " + curGroup.getGroup_No());
				parameterValue.put("userId", loginedUser.getUser_Id());
				parameterValue.put("groupNo", curGroup.getGroup_No());
				parameterValue.put("groupAnswer", group_Answer);
				
				groupService.addGroupWaiting(parameterValue);
			}
		});
	}
}

